######################################################################################################################################################
### Setup 

# Source setup file, define which congresses
setwd('~/Dropbox/Projects/Dissertation/Bill Text and Agenda Control/JOP Replication Files')
source('text_setup.R')
congs <- 104:114

# Load data
load('Data/BTAC_predNonFigures.RData')
ucH <- readRDS(file = 'Data/nonVotes_predsAll_house_uncertainty_passedOther_currentMaj_rr.rds')
ucS <- readRDS(file = 'Data/nonVotes_predsAll_senate_uncertainty_passedOther_currentMaj_rr.rds')

# Dataframe of parameters (will have to be subset for some analyses)
parms <- expand.grid(chamber=c('house', 'senate'), 
                     passedOther=c(TRUE, FALSE), 
                     currentMaj=c(TRUE, FALSE), 
                     hitchhiker=c(TRUE, FALSE), 
                     stringsAsFactors = FALSE
)

# Load hitchhiker data, pull list of hitchhiker bills
hitch <- fread('Data/Hitchhiker_Law_Pairs.csv',stringsAsFactors = FALSE, data.table = FALSE) %>%
  filter(outcome1=='insertion') %>%
  mutate(id=paste0(Cong, '_', BillType, BillNum)) %>%
  pull(id) %>% 
  unique()

# Load uncertainty list
uncertList <- readRDS('Data/uncerList.rds')



########################################################################################################################################################################
### Figure A6 (needs to be run before A3-A5)

suppHDist %<>% filter(cong!='total')
suppSDist %<>% filter(cong!='total')

# NAC failure: Didn't keep something from the floor that would HURT the majority (rolls, disses)
nfH <- with(houseqoisV, rolls+disses)/(houseqoisV$n)
nfS <- with(senqoisV, rolls+disses)/senqoisV$n

# NAC success: kept something from the floor that *would have hurt* the majority (disses, would-be rolls)
nsH <- with(houseqois, disses+dodges)/(with(houseqois, disses+dodges) + with(houseqoisV, disses+rolls) )
nsH.up <- nsH + suppHDist$dissHi + suppHDist$dodgeHi
nsH.up[nsH.up > 1] <- 1
nsH.down <- nsH - suppHDist$dissLo - suppHDist$dodgeLo
nsH.down[nsH.down < 0] <- 0

nsS <- with(senqois, disses+dodges)/(with(senqois, disses+dodges) + with(senqoisV, disses+rolls) )
nsS.up <- nsS + suppSDist$dissHi + suppSDist$dodgeHi
nsS.up[nsS.up > 1] <- 1
nsS.down <- nsS - suppSDist$dissLo - suppSDist$dodgeLo
nsS.down[nsS.down < 0] <- 1

# PAC failure: Didn't bring something to the floor that would have HELPED the majority (would-be successes, blocks??)
pfH.nb <- houseqois$missProp
pfH.nb.up <- pfH.nb + suppHDist$missHi
pfH.nb.up[pfH.nb.up > 1] <- 1
pfH.nb.down <- pfH.nb - suppHDist$missLo
pfH.nb.down[pfH.nb.down < 0] <- 0

pfS.nb <- senqois$missProp
pfS.nb.up <- pfS.nb + suppSDist$missHi
pfS.nb.up[pfS.nb.up > 1] <- 1
pfS.nb.down <- pfS.nb - suppSDist$missLo
pfS.nb.down[pfS.nb.down < 0] <- 0

pfH <- with(houseqois, misses+blocks)/houseqois$n
pfH.up <- pfH + suppHDist$missHi + suppHDist$blockHi
pfH.up[pfH.up > 1] <- 1
pfH.down <- pfH - suppHDist$missLo - suppHDist$blockLo
pfH.down[pfH.down < 0] <- 0

pfS <- with(senqois, misses+blocks)/senqois$n
pfS.up <- pfS + suppSDist$missHi + suppSDist$blockHi
pfS.up[pfS.up > 1] <- 1
pfS.down <- pfS - suppSDist$missLo - suppSDist$blockLo
pfS.down[pfS.down < 0] <- 0

# PAC success: Proportion of things helpful to the majority that were brought to the floor (successes, blocks??)
psH.nb <- houseqoisV$successes/(houseqois$misses+houseqoisV$successes)

psS.nb <- senqoisV$successes/(senqois$misses+senqoisV$successes)

psH <- with(houseqoisV, successes+blocks)/(with(houseqoisV, successes+blocks)+with(houseqois, misses+blocks))

psS <- with(senqoisV, successes+blocks)/(with(senqoisV, successes+blocks)+with(senqois, misses+blocks))


### Plot NAC success/failure

# Open graphics device
pdf(file='Figures/fgA6panelA.pdf', width=10, height=5)

par(mfrow=c(1,2), las=2)
plot(congs, nfH, type='n', ylim=c(0,1), main='Floor Bills',
     ylab='Proportion Damaging to Majority Party', xlab='Congress', xaxt='n')
grid()
lines(congs, nfH, type='b', lty=1, pch=15, lwd=1)
lines(congs, nfS, type='b', lty=2, pch=17, lwd=1)
legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
       legend=c('House', 'Senate'))
axis(side=1, at=congs)

plot(congs, nsH, type='n', xlab='Congress', ylab='Proportion Kept Off Floor', 
     ylim=c(0,1), main='Bills Damaging to Majority Party', xaxt='n')
grid()
lines(congs, nsH, type='b', pch=15, lty=1)
arrows(x0=congs, x1=congs, y0=nsH.down, y1=nsH.up, code=3, angle=90, length=0.05, lwd=2)
lines(congs, nsS, type='b', pch=17, lty=2)
arrows(x0=congs, x1=congs, y0=nsS.down, y1=nsS.up, code=3, angle=90, length=0.05, lwd=2)
legend(x='right', bty='n', legend=c('House', 'Senate'), 
       pch=c(15, 17), lty=c(1,2))
axis(side=1, at=congs)

# Close graphics device
dev.off()


### Plot PAC success/failure, including blocks

# Open graphics device
pdf(file='Figures/fgA6panelB.pdf', width=10, height=5)

par(mfrow=c(1,2), las=2)
plot(congs, pfH, type='n', ylim=c(0,1), main='Pre-Floor Bills',
     ylab='Proportion Helpful to Majority Party', xlab='Congress', xaxt='n')
grid()
lines(congs, pfH, type='b', lty=1, pch=15, lwd=1)
arrows(x0=congs, x1=congs, y0=pfH.down, y1=pfH.up, code=3, angle=90, length=0.05, lwd=2)
lines(congs, pfS, type='b', lty=2, pch=17, lwd=1)
arrows(x0=congs, x1=congs, y0=pfS.down, y1=pfS.up, code=3, angle=90, length=0.05, lwd=2)
legend(x='bottomleft', bty='n', lty=c(1,2), pch=c(15, 17), 
       legend=c('House', 'Senate'))
axis(side=1, at=congs)

plot(congs, psH, type='n', xlab='Congress', ylab='Proportion Brought to Floor', 
     ylim=c(0,1), main='Bills Helpful to Majority Party', xaxt='n')
grid()
lines(congs, psH, type='b', pch=15, lty=1)
lines(congs, psS, type='b', pch=17, lty=2)
legend(x='bottomleft', bty='n', legend=c('House', 'Senate'), 
       pch=c(15, 17), lty=c(1,2))
axis(side=1, at=congs)

# Close graphics device
dev.off()


### Plot PAC success/failure, not including blocks

# Open graphics device
pdf(file='Figures/fgA6panelC.pdf', width=10, height=5)

par(mfrow=c(1,2), las=2)
plot(congs, pfH.nb, type='n', ylim=c(0,1), main='Pre-Floor Bills',
     ylab='Proportion Helpful to Majority Party', xlab='Congress', xaxt='n')
grid()
lines(congs, pfH.nb, type='b', lty=1, pch=15, lwd=1)
arrows(x0=congs, x1=congs, y0=pfH.nb.down, y1=pfH.nb.up, code=3, angle=90, length=0.05, lwd=2)
lines(congs, pfS.nb, type='b', lty=2, pch=17, lwd=1)
arrows(x0=congs, x1=congs, y0=pfS.nb.down, y1=pfS.nb.up, code=3, angle=90, length=0.05, lwd=2)
legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
       legend=c('House', 'Senate'))
axis(side=1, at=congs)

plot(congs, psH.nb, type='n', xlab='Congress', ylab='Proportion Brought to Floor', 
     ylim=c(0,1), main='Bills Helpful to Majority Party', xaxt='n')
grid()
lines(congs, psH.nb, type='b', pch=15, lty=1)
lines(congs, psS.nb, type='b', pch=17, lty=2)
legend(x='bottomleft', bty='n', legend=c('House', 'Senate'), 
       pch=c(15, 17), lty=c(1,2))
axis(side=1, at=congs)

# Close graphics device
dev.off()



##################################################################################################################################################################################
### Functions

### Function to pull out list of pre-floor bills in each categorization
billList <- function(currentMaj=FALSE, passedOther=TRUE, hitchhiker=FALSE){
  cmVec <- ifelse(currentMaj, '_currentMaj', '')
  poVec <- ifelse(passedOther, '_passedOther', '')
  ucH <- readRDS(file = paste0('Data/nonVotes_predsAll_house_uncertainty', poVec, cmVec, '_rr.rds'))
  ucS <- readRDS(file = paste0('Data/nonVotes_predsAll_senate_uncertainty', poVec, cmVec, '_rr.rds'))
  res <- c(colnames(ucH[[1]]), colnames(ucS[[1]]))
  if(hitchhiker){
    res <- res[!res %in% hitch]
  }
  res
}

# Run
parmsBills <- parms %>%
  filter(chamber=='house') %>% # Just need one of each option for this function, chambers double it up
  dplyr::select(-chamber)
billLists <- list() 
for(ii in 1:nrow(parmsBills)){
  bills <- billList(currentMaj=parmsBills[ii,]$currentMaj, passedOther=parmsBills[ii,]$passedOther, 
                    hitchhiker=parmsBills[ii,]$hitchhiker)
  billLists[[ii]] <- bills
}



##################################################################################################################################################################################
### Configure plot data

### Function to produce data frames varying the subsetting parameters
subPreds <- function(df, billList){
  res <- df %>%
    mutate(billID=paste0(cong, '_', id)) %>%
    filter(billID %in% billList) %>%
    group_by(cong, chamber) %>%
    dplyr::summarize(
      n=n(),
      misses=sum(miss),
      disses=sum(diss),
      dodges=sum(dodge),
      blocks=sum(block),
      missProp=misses/n,
      dissProp=disses/n,
      dodgeProp=dodges/n,
      blockProp=blocks/n,
      support=misses+disses,
      supportProp=missProp+dissProp
    ) %>% 
    ungroup() %>%
    as.data.frame()
  return(res)
}

# Run
subPredsList <- list()
for(ii in 1:nrow(parmsBills)){
  bills <- billLists[[ii]]
  preds <- subPreds(df=pred, billList=bills)
  subPredsList[[ii]] <- preds
}


### Function to produce plot data
acPlotData <- function(predDF, ucDF, chmbr){
  
  # Designate voted qois
  if(chmbr=='house'){
    qoisV <- houseqoisV
  } else {
    qoisV <- senqoisV
  }
  
  # P(floor | support)
  vs <- with(qoisV, successes+disses)/(predDF$support + qoisV$support)
  vs.up <- vs + (abs(ucDF$hiU)*(predDF$n/(predDF$n+qoisV$n)))
  vs.down <- vs - (abs(ucDF$loU)*(predDF$n/(predDF$n+qoisV$n)))
  
  # P(pre-floor | oppose)
  vo <- (with(predDF, dodges+blocks)/(with(predDF, n-support)+with(qoisV, n-support)))
  vo.up <- vo + (abs(ucDF$hiU)*(predDF$n/(predDF$n+qoisV$n)))
  vo.down <- vo - (abs(ucDF$loU)*(predDF$n/(predDF$n+qoisV$n)))
  
  # Put together, return
  res <- data.frame(vs=vs, vs.up=vs.up, vs.down=vs.down,
                    vo=vo, vo.up=vo.up, vo.down=vo.down)
  return(res)
}

# Run
plotData <- list()
for(ii in 1:nrow(parms)){
  if(ii %in% 1:2){
    predDF <- subPredsList[[1]]
  } else if(ii %in% 3:4){
    predDF <- subPredsList[[2]]
  } else if(ii %in% 5:6) {
    predDF <- subPredsList[[3]]
  } else if(ii %in% 7:8){
    predDF <- subPredsList[[4]]
  } else if(ii %in% 9:10){
    predDF <- subPredsList[[5]]
  } else if(ii %in% 11:12){
    predDF <- subPredsList[[6]]
  } else if(ii %in% 13:14){
    predDF <- subPredsList[[7]]
  } else {
    predDF <- subPredsList[[8]]
  }
  predDF <- predDF[predDF$chamber==parms$chamber[ii],]
  res <- acPlotData(predDF=predDF, ucDF=uncertList[[ii]], chmbr=parms$chamber[ii])
  plotData[[ii]] <- res
}



###################################################################################################################################################################
### Figure A3


### Number and proportion of bills voted in each chamber over time
totH <- filter(pred, chamber=='house') %>% pull(cong) %>% table() %>% unname()
totS <- filter(pred, chamber=='senate') %>% pull(cong) %>% table() %>% unname()
voteH <- filter(predV, chamber=='house') %>% pull(cong) %>% table() %>% unname()
voteS <- filter(predV, chamber=='senate') %>% pull(cong) %>% table() %>% unname()
propH <- voteH/(voteH+totH)
propS <- voteS/(voteS+totS)

# Plot
pdf(file='Figures/fgA3.pdf', width=10, height=5)

par(mfrow=c(1,2), las=2)
plot(congs, voteH, type='n', xlab='Congress', ylab='Number of Bills Voted On', ylim=c(0,500), yaxt='n', xaxt='n')
abline(h=seq(0, 500, by=100), lty=3, col='grey')
lines(congs, voteH, type='b', lty=1, pch=15)
lines(congs, voteS, type='b', lty=2, pch=16)
legend(x='topleft', bty='n', legend=c('House', 'Senate'), pch=c(15, 16), lty=c(1,2))
axis(side=2, at=seq(0,500,by=100), labels=seq(0,500,by=100), las=2)
axis(side=1, at=congs)

plot(congs, propH, type='n', xlab='Congress', ylab='Proportion of Bills Voted On', ylim=c(0,0.08), xaxt='n')
abline(h=seq(0, 0.08, by=0.01), lty=3, col='grey')
lines(congs, propH, type='b', lty=1, pch=15)
lines(congs, propS, type='b', lty=2, pch=16)
axis(side=2, at=seq(0,0.08,by=0.01), labels=seq(0,0.08,by=0.01), las=2)
axis(side=1, at=congs)
legend(x='topleft', bty='n', legend=c('House', 'Senate'), pch=c(15, 16), lty=c(1,2))
par(mfrow=c(1,1), las=1)

dev.off()



###################################################################################################################################################################
### Figure A4

for(ii in 1:nrow(parmsBills)){
  
  # Which subsetting did we do?
  cm <- ifelse(parmsBills[ii,]$currentMaj, TRUE, FALSE)
  po <- ifelse(parmsBills[ii,]$passedOther, TRUE, FALSE)
  hh <- ifelse(parmsBills[ii,]$hitchhiker, TRUE, FALSE)
  
  # Specify title
  cmVec <- ifelse(cm, 'cm_', '')
  poVec <- ifelse(po, 'po_', '')
  hhVec <- ifelse(hh, 'hh_', '')
  
  # Open graphics device
  savepath <- paste0('Figures/fgA4_', poVec, cmVec, hhVec, '.pdf') %>%
    str_replace(., '_\\.pdf', '\\.pdf')
  pdf(file=savepath, width=10, height=5)
  par(las=2, mfrow=c(1,2))
  
  # Plot NAC failure
  plot(congs, houseqoisV$supportProp, type='n', ylim=c(0,1), main='Floor Bills',
       ylab='Proportion Opposed by Majority Party', xlab='Congress', xaxt='n')
  grid()
  lines(congs, 1-houseqoisV$supportProp, type='b', lty=1, pch=15, lwd=1)
  lines(congs, 1-senqoisV$supportProp, type='b', lty=2, pch=17, lwd=1)
  legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
         legend=c('House', 'Senate'))
  axis(side=1, at=congs)
  
  # Plot NAC success
  plot(congs, plotData[[2*ii-1]]$vo, type='n', xlab='Congress', ylab='Proportion Failing to Reach Floor', 
       ylim=c(0,1), main='Bills Opposed by Majority Party', xaxt='n')
  grid()
  lines(congs, plotData[[2*ii-1]]$vo, type='b', pch=15, lty=1)
  arrows(x0=congs, x1=congs, y0=plotData[[2*ii-1]]$vo.down, y1=plotData[[2*ii-1]]$vo.up, code=3, angle=90, length=0.05, lwd=2)
  lines(congs, plotData[[2*ii]]$vo, type='b', pch=17, lty=2)
  arrows(x0=congs, x1=congs, y0=plotData[[2*ii]]$vo.down, y1=plotData[[2*ii]]$vo.up, code=3, angle=90, length=0.05, lwd=2)
  legend(x='bottomleft', bty='n', legend=c('House', 'Senate'), 
         pch=c(15, 17), lty=c(1,2))
  axis(side=1, at=congs)
  
  # Reset par
  par(mfrow=c(1,1), las=1)
  
  # Turn off graphics device
  dev.off()
}



###################################################################################################################################################################
### Figure A5

# Plot
for(ii in 1:nrow(parmsBills)){
  
  # Which subsetting did we do?
  cm <- ifelse(parmsBills[ii,]$currentMaj, TRUE, FALSE)
  po <- ifelse(parmsBills[ii,]$passedOther, TRUE, FALSE)
  hh <- ifelse(parmsBills[ii,]$hitchhiker, TRUE, FALSE)
  
  # Specify title
  cmVec <- ifelse(cm, 'cm_', '')
  poVec <- ifelse(po, 'po_', '')
  hhVec <- ifelse(hh, 'hh_', '')
  
  # Open graphics device
  savepath <- paste0('Figures/fgA5_', poVec, cmVec, hhVec, '.pdf') %>%
    str_replace(., '_\\.pdf', '\\.pdf')
  pdf(file=savepath, width=10, height=5)
  par(las=2, oma=c(0, 0, 2, 0), mfrow=c(1,2))
  
  # Filter to qois objects
  houseqois <- filter(subPredsList[[ii]], chamber=='house')
  senqois <- filter(subPredsList[[ii]], chamber=='senate')
  
  # Plot PAC failure
  par(mfrow=c(1,2), las=2)
  
  plot(congs, houseqois$supportProp, type='n', ylim=c(0,1), main='Pre-Floor Bills',
       ylab='Proportion Supported by Majority Party', xlab='Congress', xaxt='n')
  grid()
  lines(congs, houseqois$supportProp, type='b', lty=1, pch=15, lwd=1)
  lines(congs, senqois$supportProp, type='b', lty=2, pch=17, lwd=1)
  arrows(x0=congs, x1=congs, y0=houseqois$supportProp-uncertList[[2*ii-1]]$loU, 
         y1=houseqois$supportProp+uncertList[[2*ii-1]]$hiU, code=3, angle=90, length=0.05, lwd=2)
  arrows(x0=congs, x1=congs, y0=senqois$supportProp-uncertList[[2*ii]]$loU, 
         y1=senqois$supportProp+uncertList[[2*ii]]$hiU, code=3, angle=90, length=0.05, lwd=2)
  legend(x='topleft', bty='n', lty=c(1,2), pch=c(15, 17), 
         legend=c('House', 'Senate'))
  axis(side=1, at=congs)
  
  # Plot PAC success
  plot(congs, plotData[[2*ii-1]]$vs, type='n', xlab='Congress', ylab='Proportion Reaching Floor', 
       ylim=c(0,1), main='Bills Supported by Majority Party', xaxt='n')
  grid()
  lines(congs, plotData[[2*ii-1]]$vs, type='b', pch=15, lty=1)
  arrows(x0=congs, x1=congs, y0=plotData[[2*ii-1]]$vs.down, y1=plotData[[2*ii-1]]$vs.up, code=3, angle=90, length=0.05, lwd=2)
  lines(congs, plotData[[2*ii]]$vs, type='b', pch=17, lty=2)
  arrows(x0=congs, x1=congs, y0=plotData[[2*ii]]$vs.down, y1=plotData[[2*ii]]$vs.up, code=3, angle=90, length=0.05, lwd=2)
  legendpos <- ifelse(ii %in% c(2, 4, 6), 'topleft', 'bottomleft')
  legend(x=legendpos, bty='n', legend=c('House', 'Senate'),
         pch=c(15, 17), lty=c(1,2))
  axis(side=1, at=congs)
  
  par(mfrow=c(1,1), las=1) 
  
  # Turn off Graphics device
  dev.off()
}
